package org.dtrd.modules.ocr.service.impl;

import org.dtrd.modules.ocr.entity.po.DtrdEntRdOcrMappingTable;
import org.dtrd.modules.ocr.mapper.DtrdEntRdOcrMappingTableMapper;
import org.dtrd.modules.ocr.service.IDtrdEntRdOcrMappingTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.ocr.entity.bean.OcrMappingTableDetail;
import org.dtrd.modules.ocr.entity.bean.OcrMappingTableListInfo;
import org.dtrd.modules.ocr.entity.bean.OcrMappingTableListRequest;
import org.dtrd.modules.ocr.entity.bean.OcrMappingTablePageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-09-07
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdOcrMappingTableServiceImpl extends ServiceImpl<DtrdEntRdOcrMappingTableMapper, DtrdEntRdOcrMappingTable> implements IDtrdEntRdOcrMappingTableService {

    private DtrdEntRdOcrMappingTableMapper dtrdEntRdOcrMappingTableMapper;

    @Override
    public List<OcrMappingTableListInfo> getOcrMappingTableList(OcrMappingTableListRequest request) {
        LambdaQueryWrapper<DtrdEntRdOcrMappingTable> queryWrapper = baseQueryWrapper();
        List<DtrdEntRdOcrMappingTable> poList = list(queryWrapper);
        List<OcrMappingTableListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(OcrMappingTableListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<OcrMappingTableListInfo> pageOcrMappingTable(OcrMappingTablePageRequest request) {
        LambdaQueryWrapper<DtrdEntRdOcrMappingTable> queryWrapper = baseQueryWrapper();
        IPage<DtrdEntRdOcrMappingTable> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(OcrMappingTableListInfo::new);
    }

    @Override
    public OcrMappingTableDetail getOcrMappingTable(Integer dataId) {
        DtrdEntRdOcrMappingTable dtrdEntRdOcrMappingTable = getById(dataId);
        return Optional.ofNullable(dtrdEntRdOcrMappingTable)
                        .map(po -> new OcrMappingTableDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateOcrMappingTable(OcrMappingTableDetail dto) {
        DtrdEntRdOcrMappingTable dtrdEntRdOcrMappingTable = new DtrdEntRdOcrMappingTable();
        if(dto.getDataId() == null){
            dtrdEntRdOcrMappingTable = dtrdEntRdOcrMappingTable.init();
        }
        dtrdEntRdOcrMappingTable.parseFromDto(dto);
        return saveOrUpdate(dtrdEntRdOcrMappingTable);
    }

    @Override
    public boolean removeOcrMappingTable(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdOcrMappingTable> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdOcrMappingTable.class)
                        .eq(DtrdEntRdOcrMappingTable::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdOcrMappingTable> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdOcrMappingTable.class)
                        .eq(DtrdEntRdOcrMappingTable::getIsDel, 0);
    }
}
